import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    defaultData: {
      title: "售后详情", // 导航栏标题
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    showAfterSales: false,
    choice: false,
    checkedItems: [], // 记录选中的项目
    imageURL: "https://zntg.swehszn.com/file/WechatIMG637.png",
    afterSalesOrder: {},
    checkedAll: false,
    isAll:true,
    orderId:null
  },

  onLoad(options) {
    this.getAfterSalesOrder()
  },

  onShow() {

  },

  // 获取售后订单
  getAfterSalesOrder() {
    const afterSalesOrder = wx.getStorageSync('afterSalesOrder');
    this.setData({
      afterSalesOrder,
      orderId:afterSalesOrder.id
    })
    console.log("this", this.data.afterSalesOrder);
    wx.removeStorageSync('afterSalesOrder');
  },

  bottomApplyAfterSales() {
    if (this.data.checkedItems.length == 0) {
      wx.showToast({
        title: '请选择需要售后的商品',
        icon: 'none'
      });
    } else {
      const selectedItems = this.data.afterSalesOrder.products.filter(product => this.data.checkedItems.includes(String(product.id)));
      console.log("选择的商品", selectedItems);
      wx.setStorageSync('afterSalesProducts', selectedItems);
      wx.navigateTo({
        url: `/packageUserCenter/pages/writeSales/index?orderId=${this.data.orderId}&orderNo=${this.data.afterSalesOrder.orderNo}`
      })
    }
  },

  applyAfterSales(event) {
    console.log("productsItem", event.currentTarget.dataset.productsitem);
    const selectedItems = [event.currentTarget.dataset.productsitem]
    wx.setStorageSync('afterSalesProducts', selectedItems);
    wx.navigateTo({
      url: `/packageUserCenter/pages/writeSales/index?orderId=${this.data.orderId}&orderNo=${this.data.afterSalesOrder.orderNo}`
    })
  },

  multipleChoice() {
    this.setData({
      choice: !this.data.choice
    });
  },

  selectAll(event) {
    const checked = event.detail;
    let newCheckedItems = [];
    const isALL=false
    if (checked) {
      newCheckedItems = this.data.afterSalesOrder.products.map(item => String(item.id));
    }
    console.log("newCheckedItems",newCheckedItems);
    if (newCheckedItems.length>0) {
      this.setData({
        isAll:false
      })
    }else{
      this.setData({
        isAll:true
      })
    }
    this.setData({
      checkedItems: newCheckedItems,
      checkedAll: checked,
      
    });
  },

  stepper(){},

  goodsCountChande(e) {
    const { id } = e.currentTarget.dataset; 
    const newCount = e.detail;            
    console.log("eeeee",e);
    const updatedProducts = this.data.afterSalesOrder.products.map(product => {
        if (product.id === id) {
            product.goodsCount = newCount;   
            product.goodsSubtotal = newCount * Number(product.goodsPrice);
        }
        return product;
    });

    this.setData({
        'afterSalesOrder.products': updatedProducts
    });

    console.log("afterSalesOrder.products",this.data.afterSalesOrder.products);
},

  onCheckboxChange(event) {
    const checkedItems = event.detail;
    const allChecked = checkedItems.length == this.data.afterSalesOrder.products.length;
    this.setData({
      checkedItems,
      checkedAll: allChecked,
    });
    console.log("checkedItems",checkedItems);
  },

  // 复制订单编号
  copyOrderId(e) {
    const orderId = e.currentTarget.dataset.orderId;
    wx.setClipboardData({
      data: orderId,
      success: function () {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        });
      }
    });
  }

})